package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.VideoDetailActivity;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class JiLuPianAdapter extends BaseAdapter {
    private Context mContext;
    private List<VideoInfo> mList = new ArrayList<VideoInfo>();

    public JiLuPianAdapter(Context context){
        mContext = context;
    }

    public void updateData(List<VideoInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(mList != null){
            mList.clear();
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList != null && mList.size() > 0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null && mList.size() >0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.jilupian_item, null);
            childHolder.videoImage = (ImageView) convertView.findViewById(R.id.videologo);
            childHolder.videoTime = (TextView)convertView.findViewById(R.id.time);
            childHolder.videoTitle = (TextView) convertView.findViewById(R.id.videotitle);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder)convertView.getTag();
        }

        final VideoInfo info = mList.get(position);
        if(info != null){
            String vidoeIconUrl = info.getVideoLogo();
            if(vidoeIconUrl != null && vidoeIconUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.videoImage, vidoeIconUrl, null);
            }
            childHolder.videoTime.setText(info.getVideoDuration());
            childHolder.videoTitle.setText(info.getVideoTitle());

            childHolder.videoImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uId = info.getUserId();
                    String videoId = info.getVideoId();
                    String youkuVideoId = info.getPath();
                    Intent intent = new Intent(mContext,VideoDetailActivity.class);
                    intent.putExtra("videoid",videoId);
                    intent.putExtra("UID",uId);
                    intent.putExtra("FEE", info.getFee());
                    intent.putExtra("YOUKUVIDEOID",youkuVideoId);
                    intent.putExtra("ZUOZHE", info.getUserName());
                    mContext.startActivity(intent);

                }
            });
        }
        return convertView;
    }

    class ChildHolder{
        ImageView videoImage;
        TextView videoTime;
        TextView videoTitle;
    }


}
